home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1288 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: char* to char array[] ?
  5. Date: 12 Jan 1996 21:08:58 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4d6ila$g4r@news.iag.net>
  8. References: <4d4n9s$9uv@ixnews2.ix.netcom.com>
  9. NNTP-Posting-Host: pm1-orl25.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4d4n9s$9uv@ixnews2.ix.netcom.com>, tstevens@ix.netcom.com says...
  13. >
  14. >        My situation is this: I have a routine which needs to return a 
  15. >char string:
  16. >
  17. >char * called_function() {
  18.  
  19. You will find several solutions (direct and indirect) for this in the c.l.c 
  20. faq (Frequently Asked Question) list.  It is available for anonymous ftp
  21. from rtfm.mit.edu /pub/usenet/comp.lang.c.
  22.  
  23. Basically, you can:
  24.  
  25.   1. Have the function malloc a char array and return a pointer to it.
  26.      The caller will have to free this memory later.
  27.  
  28.   2. Define a static char array within the function and return a pointer
  29.      to it.  Since only one copy of the array will exist, any modification
  30.      made to it will appear at all pointers returned by the function.
  31.  
  32.   3. define a global char array and have the function return a pointer
  33.      to it.  Same problem as above.
  34.  
  35.   4. Define an automatic char array within the function, return a pointer
  36.      to it, and see how long it takes to crash the program. :-)
  37.  
  38. -- 
  39. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  40. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  41.  
  42.